package org.springdata.chap1.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;
import javax.swing.tree.RowMapper;

import org.omg.CORBA.PRIVATE_MEMBER;
import org.springdata.chap1.model.CircleDia;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class JDBCDAO {
	//talk to database

	
	private DataSource datasource;
	private JdbcTemplate jdbctemplate=new JdbcTemplate();

	
	
	
	
	
	



public DataSource getDatasource() {
		return datasource;
	}
@Autowired
	public void setDatasource(DataSource datasource) {
		this.jdbctemplate=new JdbcTemplate(datasource);
	}


public JdbcTemplate getJdbctemplate() {
		return jdbctemplate;
	}

	public void setJdbctemplate(JdbcTemplate jdbctemplate) {
		this.jdbctemplate = jdbctemplate;
	}



public int getCirclecount(){
	String sql="Select count(*) from circle";	
	return jdbctemplate.queryForInt(sql); 	
}

public String getCirclename(int circleid){
	String sql="Select name from circle where id=?";	
	
	return jdbctemplate.queryForObject(sql,new Object[] {circleid} ,String.class);
}
public CircleDia getCircleforid(int circleid){
	String sql="Select * from circle where id=?";
	
	return jdbctemplate.queryForObject(sql,new Object[] {circleid} ,new circlemapper());
}


public  void setcircle(int circleid,String circlename){
	String sql = "INSERT INTO CIRCLE (ID, NAME) VALUES (?, ?)";	
	 jdbctemplate.update(sql,new Object[] {circleid,circlename} );
}






public CircleDia	setCircleDia(int circleId) {
	
	Connection conn=null;

	
	
	
	
	try {
	

		conn=datasource.getConnection();


		
//		
//		String sql = "INSERT INTO CIRCLE " +
//				"(ID, NAME) VALUES (?, ?)";
//		
//		
//		PreparedStatement ps1 = conn.prepareStatement(sql);
//		ps1.setInt(1,2);
//		ps1.setString(2, "twoo");
//		ps1.executeUpdate();
//		ps1.close();
//		
		
		
		
		
		PreparedStatement ps=conn.prepareStatement("Select * from CIRCLE");
	//	ps.setInt(2, circleId);
	
		CircleDia c1=null;
	//	c1=new CircleDia(2,"second");
		ResultSet rs=ps.executeQuery();
	
	if (rs.next())
	{
		c1=new CircleDia( Integer.parseInt(rs.getString("id")), rs.getString("name"));
		System.out.println( rs.getString("name"));
	}
	
	
	
	rs.close();
	ps.close();
return c1;	
	
	
	
	
	} catch (Exception e) {
		// TODO Auto-generated catch block	
		System.out.println("exception called");
		e.printStackTrace();
	}
	return null;
	
	
}






private static final class	 circlemapper implements org.springframework.jdbc.core.RowMapper<CircleDia>{
 
	


@Override
public CircleDia mapRow(ResultSet resultset, int rownum) throws SQLException {
	// TODO Auto-generated method stub
	CircleDia c1=new CircleDia();
	c1.setId(resultset.getInt("id"));
	c1.setName(resultset.getString("name"));
	return c1;
}}




}
